package com.qingzhuge.file.service.cloud;


import com.qingzhuge.context.SpringContext;
import com.qingzhuge.file.FileStorageEnum;
import com.qingzhuge.file.entity.FileStorageConfig;
import com.qingzhuge.file.service.BaseFileStorageService;
import com.qingzhuge.sys.service.ISysConfigService;
import org.apache.commons.lang3.StringUtils;

import static com.qingzhuge.file.entity.FileStorageConfig.CLOUD_STORAGE_CONFIG_KEY;

/**
 * @author : zero.xiao
 * @description : 文件上传Factory
 * @date :2018/6/8 0008 下午 15:14
 * @modified :
 * @since : 1.0.0
 */
public final class FileStorageFactory {
    private static ISysConfigService sysConfigService;

    static {
        FileStorageFactory.sysConfigService = SpringContext.getBean(ISysConfigService.class);
    }

    public static BaseFileStorageService buildLocal(String localPrefix) {
        FileStorageConfig config = new FileStorageConfig();
        if (StringUtils.isNotBlank(localPrefix)) {
            config.setLocalPrefix(localPrefix);
        }
        return new StorageLocalService(config);
    }

    public static BaseFileStorageService build() {
        //获取存储配置信息
        FileStorageConfig config = sysConfigService.getConfigObject(CLOUD_STORAGE_CONFIG_KEY, FileStorageConfig.class);
        if (null != config) {
            if (config.getType() == FileStorageEnum.QINIU) {
                return new StorageQiniuCloudService(config);
            } else if (config.getType() == FileStorageEnum.ALIYUN) {
                return new StorageAliyunCloudService(config);
            } else {
                return new StorageLocalService(config);
            }
        }
        config = new FileStorageConfig();
        return new StorageLocalService(config);
    }

}
